
#define _STX_ETX_C_

#include "config.h"
#include "typedefs.h"
#include "stx_etx.h"

//****************************************************************************/
//** Function Name :                                                        **/
void ConvertChar2HexAscii(
  unsigned char ucByte,       //!< character to convert
  unsigned char *pH,          //!< upper half converted to ascii
  unsigned char *pL)          //!< lower half converted to ascii

/*! \brief Convert hex to hexascii
 *
 *  converts 1byte hex char in 2bytes char as hexascii
 *
 */
//** Author        : 
//** Date          : 
//****************************************************************************/
{
  unsigned char ucH = 0;
  unsigned char ucL = 0;
  
  ucH = ucByte >> 4;    // /16
  ucL = ucByte & 0x0f;  // % 16
  if ( ucH > 9 ) {
    *pH = 'A' + ucH - 10;
  } else {
    *pH = '0' + ucH;
  }
  if ( ucL > 9 ) {
    *pL = 'A' + ucL - 10;
  } else {
    *pL = '0' + ucL;
  }
}

//****************************************************************************/
//** Function Name :                                                        **/
void ConvertHexAscii2Char(
  unsigned char ucHexAsciiH,  //!< upper half of destination char
  unsigned char ucHexAsciiL,  //!< lower half of destination char
  unsigned char *pC)          //!< destination char

/*! \brief Convert hexascii to ascii
 *
 *  converts 0x33 0x31 to 0x31; '3' '1' to '1'
 *
 */
//** Author        : 
//** Date          : 
//****************************************************************************/
{
  unsigned char ucH = 0;
  unsigned char ucL = 0;
  
  if ( '0' <= ucHexAsciiH && ucHexAsciiH <= '9' ) {
    ucH = ucHexAsciiH - '0';
  } else if ( 'a' <= ucHexAsciiH && ucHexAsciiH <= 'f'  ) {
    ucH = ucHexAsciiH - 'a' + 10;
  } else if ( 'A' <= ucHexAsciiH && ucHexAsciiH <= 'F'  ) {
    ucH = ucHexAsciiH - 'A' + 10;
  } else {
    ucH = 0;
  }

  if ( '0' <= ucHexAsciiL && ucHexAsciiL <= '9' ) {
    ucL = ucHexAsciiL - '0';
  } else if ( 'a' <= ucHexAsciiL && ucHexAsciiL <= 'f'  ) {
    ucL = ucHexAsciiL - 'a' + 10;
  } else if ( 'A' <= ucHexAsciiL && ucHexAsciiL <= 'F'  ) {
    ucL = ucHexAsciiL - 'A' + 10;
  } else {
    ucL = 0;
  }
  
  *pC = ( ucH << 4 ) + ucL;
}
//****************************************************************************/
//** Function Name :                                                        **/
void STXETX_ConvertPacket2Command(
  T_STXETX_PACKET *pPacket,   //!< from packet in ascii hex
  T_CMD *pBlock)              //!< destination command in hex

/*! \brief Convert hex to hexascii
 *
 *  command handler execution function
 *
 */
//** Author        : 
//** Date          : 
//****************************************************************************/
{
  unsigned int j = 0;
  unsigned int i = 0;
  unsigned char ucHexAsciiH = 0;
  unsigned char ucHexAsciiL = 0;
  unsigned char ucByte = 0;
  
  if ( pPacket && pBlock ) {
    for ( i = 1; i + 2 < pPacket->uiLen; i += 2 )
    {
      ucHexAsciiH = pPacket->data[ i ];
      ucHexAsciiL = pPacket->data[ i + 1 ];
      
      ConvertHexAscii2Char( ucHexAsciiH, ucHexAsciiL, &ucByte );
    
      pBlock->data[ j++ ] = ucByte;
    }
  
    pBlock->ucLen = j;
  }
}

//****************************************************************************/
//** Function Name :                                                        **/
void STXETX_ConvertCommand2Packet(
  T_CMD *pCommand,            //!< pointer to command in hex
  T_STXETX_PACKET *pPacket)   //!< destination in ascii hex

/*! \brief Convert hex to hexascii
 *
 *  command handler execution function
 *
 */
//** Author        : 
//** Date          : 
//****************************************************************************/
{
  unsigned int j = 0;
  unsigned int i = 0;
  unsigned char ucHexAsciiH = 0;
  unsigned char ucHexAsciiL = 0;
  unsigned char ucByte = 0;
//  static unsigned int crc16Value = 0;
  
  if ( pCommand && pPacket ) {
    pPacket->data[ j++ ] = C_STXETX_STX;
    
    for ( i = 0; i < pCommand->ucLen; ++i )
    {
      ucByte = pCommand->data[ i ];
      ConvertChar2HexAscii( ucByte, &ucHexAsciiH, &ucHexAsciiL );
          
      pPacket->data[ j++ ] = ucHexAsciiH;
      pPacket->data[ j++ ] = ucHexAsciiL;
    }
	
    #ifdef USE_SERIAL_CRC
/*      
      crc16Value = crc16(&(pPacket->data[1]), (pCommand->uiLen)*2);
	  ucByte = (unsigned char)(crc16Value & 0xFF);
      ConvertChar2HexAscii( ucByte, &ucHexAsciiH, &ucHexAsciiL );
      pPacket->data[ j++ ] = ucHexAsciiH;
      pPacket->data[ j++ ] = ucHexAsciiL;
	  
	  ucByte = (unsigned char)(crc16Value / 0x100);
      ConvertChar2HexAscii( ucByte, &ucHexAsciiH, &ucHexAsciiL );
      pPacket->data[ j++ ] = ucHexAsciiH;
      pPacket->data[ j++ ] = ucHexAsciiL;
*/	  
    #endif
    pPacket->data[ j++ ] = C_STXETX_ETX;
    pPacket->uiLen = j;
  }
}


#undef _STX_ETX_C_
